//* player's ship

ship_img = new Array();
ship_img[0] = new Image();
ship_img[0].src = 'img/ship/ship0.png';
ship_img[1] = new Image();
ship_img[1].src = 'img/ship/ship1.png';
ship_img[2] = new Image();
ship_img[2].src = 'img/ship/ship2.png';
ship_img[3] = new Image();
ship_img[3].src = 'img/ship/ship3.png';

function Ship() {
  // create ship
  this.x = 0;
  this.y = 0;
  this.dx = 10;
  this.dy = 0;
  this.speed = 0;
  this.image = 0;
  this.angle = 0;
  this.credit = 1500;
  this.life = 100;
  this.magnet = 16;
  this.beams = 1;
  this.range = 150;
  this.rate = 120;  // 60'000ms = 60s / 120 rpm = 500 ms timer
  this.spread = 1;
  this.acceleration = 1.5;
  this.max_speed = 5;
  this.credit_multiplier = 1;
  this.penetration = 2;
  
  // wasd movement
  this.wasd = function() {
    // game over?
    if (this.life <= 0)
      return false;
    // update ship speed according to pressed keys
    if (key[87]) this.dy -= this.acceleration; // W
    if (key[65]) this.dx -= this.acceleration; // A
    if (key[83]) this.dy += this.acceleration; // S
    if (key[68]) this.dx += this.acceleration; // D
    // speed limiter
    if (this.dx > this.max_speed) this.dx = this.max_speed;
    if (this.dy > this.max_speed) this.dy = this.max_speed;
    if (this.dx < -this.max_speed) this.dx = -this.max_speed;
    if (this.dy < -this.max_speed) this.dy = -this.max_speed;
    // move background offset
    this.x += this.dx;
    this.y += this.dy;
    // return true if ship is moving
    var ship_is_moving = (this.dx != 0)||(this.dy != 0); 
    // slowing down without thrusters
    this.dx *= 0.95;
    this.dy *= 0.95;
/*    if (this.dx > 0) this.dx -= this.acceleration/2;
    if (this.dx < 0) this.dx += this.acceleration/2;
    if (this.dy > 0) this.dy -= this.acceleration/2;
    if (this.dy < 0) this.dy += this.acceleration/2;*/
    // done 
    return ship_is_moving;
  }
  
  // ship rendering
  this.draw = function() {
    // credits indicator
    context.save();
    context.font = "16pt courier";
    context.textAlign = "left";
    context.fillStyle = "yellow";
    context.fillText("$ "+this.credit,10,480-10);
    context.restore();          
    // life indicator
    var z = Math.max(0,this.life);
    z = Math.min(100,z);
    context.fillStyle = "rgba(0,255,0,0.5)";
    context.fillRect(640-100-8,480-10-8,z,10);
    context.fillStyle = "rgba(255,0,0,0.5)";
    context.fillRect(640-100+z-8,480-10-8,100-z,10);
    // game over?
    if (this.life > 0) {
      // calculate ships speed and corresponding image
      this.speed = this.dx*this.dx+this.dy*this.dy;
      this.image = 0;
      if (this.speed > 0.4*this.max_speed) this.image = 1;
      if (this.speed > 0.6*this.max_speed) this.image = 2;
      if (this.speed > 0.8*this.max_speed) this.image = 3-Math.round(Math.random());
      // draw ship
      context.save();
      context.translate(-x0,-y0);
      context.rotate(this.angle);
      //  context.clearRect(-8,-8,16,16);
      context.drawImage(ship_img[this.image],-8,-8);
      context.restore();
    }
  }
  
  // debug function
  this.debug = function() {
    return 'Ship: x='+this.x+' y='+this.y+'\n\n';
  }
}
